package com.x.bff.app.service.mc;

import com.x.bff.app.model.vo.mc.SendImRequestVO;
import com.x.core.utils.BeanUtil;
import com.x.provider.api.customer.enums.CustomerResourceOperBizScene;
import com.x.provider.api.customer.model.dto.customer.CustomerResourceOperReqDTO;
import com.x.provider.api.customer.service.CustomerRpcService;
import com.x.provider.api.mc.model.dto.MessageDTO;
import com.x.provider.api.mc.model.dto.MessageSendReqDTO;
import com.x.provider.api.mc.model.protocol.MessageClassEnum;
import com.x.provider.api.mc.service.MessageRpcService;
import org.springframework.stereotype.Service;

@Service
public class MessageServiceImpl implements MessageService{

    private final MessageRpcService messageRpcService;
    private final CustomerRpcService customerRpcService;

    public MessageServiceImpl(MessageRpcService messageRpcService,
                              CustomerRpcService customerRpcService){
        this.messageRpcService = messageRpcService;
        this.customerRpcService = customerRpcService;
    }

    @Override
    public MessageDTO sendMessage(SendImRequestVO sendImRequestVO) {
        CustomerResourceOperReqDTO customerResourceOperReqDTO = CustomerResourceOperReqDTO.builder().customerId(sendImRequestVO.getCustomerId())
                .resourceId(CustomerResourceOperBizScene.CustomerResourceDescBizSceneEnum.DESC_IM_SEND_COUNT_4_SEND_ONE.getId()).tryOper(true).count(-1L).build();
//        Boolean canSend = customerRpcService.operateCustomerResource(customerResourceOperReqDTO).getData();
//        ApiAssetUtil.isTrue(canSend, AppErrorCode.IM_SEND_COUNT_RUN_OUT);
        MessageSendReqDTO reqDTO = BeanUtil.prepare(sendImRequestVO, MessageSendReqDTO.class);
        reqDTO.setFromCustomerId(sendImRequestVO.getCustomerId());
        reqDTO.setMessageClass(MessageClassEnum.IM.getValue());
        reqDTO.setOnlineUserOnly(false);
        MessageDTO result = messageRpcService.sendMessage(reqDTO).getData();
        customerResourceOperReqDTO.setTryOper(false);
        customerRpcService.operateCustomerResource(customerResourceOperReqDTO).getData();
        return result;
    }

}
